import java.util.PriorityQueue;
import java.util.concurrent.*;

class MyTimerTask{
    Runnable runnable;
    long time;

    public MyTimerTask(Runnable runnable, long time) {
        this.runnable = runnable;
        this.time = System.currentTimeMillis() + time;
    }

    public Runnable getRunnable() {
        return runnable;
    }

    public long getTime() {
        return time;
    }
}

class MyTimer{
    private PriorityQueue<MyTimerTask> queue = new PriorityQueue<>();
    private Object locker = new Object();

    public void submit(Runnable runnable, long time){
        synchronized (locker){
            queue.offer(new MyTimerTask(runnable, time));
            locker.notify();
        }
    }

    public MyTimer() {
        Thread thread = new Thread(() -> {
            while(true){
                synchronized (locker){
                    while(queue.isEmpty()){
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    MyTimerTask task = queue.peek();
                    long curTime = System.currentTimeMillis();
                    if(curTime >= task.getTime()){
                        task.getRunnable().run();
                        queue.poll();
                    }else{
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }

                }
            }
        });
        thread.start();
    }
}

public class Demo1 {
    public static void main1(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(1);
        service.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println(1);
            }
        });
    }

    public static void main(String[] args) {

    }
}
